package controller_admin

import (
	"github.com/doujunyu/gogo/job"
	"github.com/doujunyu/gogo/sql_aid"
	"go_self/model"
	"go_self/server"
	"go_self/util"
)

func LoginByUsername(j *job.Job){
	if j.Input["username"] == "" {
		j.JsonSuccess(nil, "账号不能为空")
		return
	}
	if j.Input["password"] == "" {
		j.JsonSuccess(j.Input, "密码不能为空")
		return
	}

	userSql, args := sql_aid.PgTable(model.TableByAdmin).Field("id", "status", "password").Where("username = ?", j.Input["username"]).ToSql()
	admin, err := sql_aid.DataToMap(model.PgSql.Query(userSql, args...))
	if err != nil || len(admin) == 0 {
		j.JsonError(nil, "账户不存在")
		return
	}
	if int(admin[0]["status"].(int64)) != model.UserByStatusNormal {
		j.JsonError(nil, "账户已被禁用")
		return
	}

	if err = util.CheckHash(admin[0]["password"].(string), j.Input["password"]); err != nil {
		j.JsonError(nil, "密码错误")
		return
	}

	data, err := server.AdminLogin(admin[0]["id"].(int64))
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}
	j.JsonSuccess(data)

}





